package com.atguigu.client;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

public class Hbase_Single_Client {
    //声明一个连接属性
    public static Connection connection =null;

    //静态代码块 会随着类的加载而加载
    static {
        //如果链接为空的话 则初始化链接
        if (connection==null){
            try {
                //通过客户端工厂创建链接
                connection = ConnectionFactory.createConnection();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 关闭链接的方法
     */
    public static void close(){
        if (connection!=null){ //如果链接对象不是空的再关闭
            try {
                connection.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //测试
    public static void main(String[] args) {
        Connection connection1 = Hbase_Single_Client.connection;
        System.out.println(connection1);
        close();
    }
}
